package com.hoyo.config;


import java.io.Serializable;

import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.authc.credential.HashedCredentialsMatcher;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.util.SimpleByteSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.hoyo.domain.User;
import com.hoyo.domain.UserInfo;
import com.hoyo.service.ModuleService;
import com.hoyo.service.UserService;

/**
 * 验证用户登录
 * 
 * @author Administrator
 */
@Component("userRealm")
public class UserRealm extends AuthorizingRealm {
	@Autowired
	private UserService userService;
	
	@Autowired
	private ModuleService moduleService;

	public UserRealm() {
		setName("UserRealm");
		// 采用MD5加密
		setCredentialsMatcher(new HashedCredentialsMatcher("md5"));
	}

	//权限资源角色
	@Override
	protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
		Principal principal = (Principal) principals.getPrimaryPrincipal();
		SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
		//add Permission Resources
		try {
//			info.setStringPermissions(userService.findPermissions(principal.toString()));
			info.setRoles(moduleService.findRolesByAccount(principal.toString()));
		} catch (Exception e) {
			e.printStackTrace();
		}
		//add Roles String[Set<String> roles]
		//info.setRoles(roles);
		return info;
	}
	
	//登录验证
	@Override
	protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException{
		UsernamePasswordToken upt = (UsernamePasswordToken) token;
		String identifier = upt.getUsername();
		User user = null;
		try {
			user = userService.findByIdentifier(identifier);
		} catch (Exception e) {
			e.printStackTrace();
		}

		if (user == null) {
			throw new UnknownAccountException();
		}
		SimpleAuthenticationInfo info = new SimpleAuthenticationInfo(new Principal(user,userService.findUserInfoByUserId(user.getId())), user.getPassword(), new SimpleByteSource(MainFinalConfig.SALT), getName());
		return info;
	}
	
	public static void main(String[] args) {
		
		System.out.println(new Md5Hash("12121122", new SimpleByteSource(MainFinalConfig.SALT)));
	}
	
	
	public static class Principal implements Serializable {

		private static final long serialVersionUID = 1L;

		private User user;
		private UserInfo userInfo;
		
		public UserInfo getUserInfo() {
			return userInfo;
		}

		public void setUserInfo(UserInfo userInfo) {
			this.userInfo = userInfo;
		}

		public User getUser() {
			return user;
		}

		public void setUser(User user) {
			this.user = user;
		}

		public Principal(User user,UserInfo userInfo) {
			this.user=user;
			this.userInfo = userInfo;
		}


		@Override
		public String toString() {
			return this.user.getPhone();
		}

	}
}





